from meiduo_admin.serializer.spu_serializer import *
from rest_framework.generics import GenericAPIView
from meiduo_admin.pages import MyPage
from goods.models import *
from rest_framework.response import Response


# 请求方式： GET /meiduo_admin/goods/?keyword=<名称|副标题>&page=<页码>&page_size=<页容量>
class SPUGoodsView(GenericAPIView):
    serializer_class = SPUGoodsSerializer
    pagination_class = MyPage
    queryset = SPU.objects.all()

    def get(self, request):
        """展示所有商品信息"""
        spu_queryset = self.get_queryset()
        # 分页
        page = self.paginate_queryset(spu_queryset)
        # 判断
        if page:
            # 对这个子集进行序列化操作
            page_serializer = self.get_serializer(page, many=True)
            # 参数返回的是子集序列化后得到的数据
            return self.get_paginated_response(page_serializer.data)
        serializer = SPUGoodsSerializer(spu_queryset, many=True)
        return Response(serializer.data)

    def post(self, request):
        """保存SPU数据"""
        spu_obj = self.request.data
        serializer = self.get_serializer(data=spu_obj)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=201)


# 请求方式： GET /meiduo_admin/goods/brands/simple
class SPUBrandView(GenericAPIView):
    """品牌"""
    serializer_class = SPUBrandsSerizliser
    queryset = Brand.objects.all()

    def get(self, request):
        goods_brand = self.get_queryset()
        print(goods_brand)
        serializer = SPUBrandsSerizliser(goods_brand, many=True)
        print(serializer)
        return Response(serializer.data)


# 请求方式： GET meiduo_admin/goods/channel/categories/
class ChannelCategorysView(GenericAPIView):
    """一级分类"""
    serializer_class = CategorysSerizliser
    queryset = GoodsCategory.objects.filter(parent=None)

    def get(self, request):
        classify = self.get_queryset()
        serializer = self.get_serializer(classify, many=True)
        return Response(serializer.data)


# 请求方式： GET /meiduo_admin/goods/channel/categories/(?P<pk>\d+)/
class ChannelCategoryView(GenericAPIView):
    """二三级分类"""
    serializer_class = CategorysSerizliser
    queryset = GoodsCategory.objects.all()

    def get(self, request, pk):
        spu_obj = GoodsCategory.objects.filter(parent=pk)
        serializer = self.get_serializer(spu_obj, many=True)
        print(serializer.data)
        return Response(serializer.data)


# 请求方式： GET /meiduo_admin/goods/(?P<pk>\d+)/
class SKUGoods_View(GenericAPIView):
    serializer_class = SPUGoodsSerializer
    queryset = SPU.objects.all()

    # 请求方式： GET/meiduo_admin/goods/(?P < pk > \d+)/
    def get(self, request, pk):
        sku_queryset = SPU.objects.get(pk=pk)
        serializer = SPUGoodsSerializer(sku_queryset)
        return Response(serializer.data)

    # 请求方式： PUT /meiduo_admin/goods/(?P<pk>\d+)/
    def put(self, request, pk):
        spu_obj = self.get_object()
        serializer = SPUGoodsSerializer(instance=spu_obj, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    # 请求方式： Delte meiduo_admin/goods/(?P<pk>\d+)/
    def delete(self, request, pk):
        spu_obj = self.get_object()
        spu_good = spu_obj.delete()
        return Response(spu_good)